//
//  CinemaViewController.m
//  豆瓣
//
//  Created by dllo on 15/12/7.
//  Copyright © 2015年 dllo. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaTableViewCell.h"
#import "AppTools.h"
#import "Cinema.h"
#import "AFNetworking.h"

@interface CinemaViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *myTableView;
@property (nonatomic, retain) NSMutableArray *allArray;


@end

@implementation CinemaViewController

- (void)dealloc{
    [_allArray release];
    [_myTableView release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"影院";
    
    [self afnGetRequest];
    self.myTableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    
    [self.myTableView registerClass:[CinemaTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    [self.view addSubview:_myTableView];
    [_myTableView release];
//    
//    [AppTools getDataFromJsonWithFileName:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php" Block:^(id result) {
//        
//        NSMutableDictionary *dic = result;
//        
//        NSMutableArray *array = [dic objectForKey:@"data"];
//        
//        for (NSDictionary *dic in array) {
//            Cinema *cinema = [[Cinema alloc] init];
//            [cinema setValuesForKeysWithDictionary:dic];
//            [self.allArray addObject:cinema];
//            
//            [cinema release];
//            
//        }
//        
//    } error:^(NSError *error) {
//        
//        
//        
//    }];
//    
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    Cinema *cinema = [self.allArray objectAtIndex:indexPath.row];
    cell.cinema = cinema;
    
//    cell.textLabel.text = @"大水杯";
    
    return cell;
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.allArray.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return 150;
}

-(void)afnGetRequest
{
    
    NSString *url_string = [NSString stringWithFormat:@"http://project.lanou3g.com/teacher/yihuiyun/lanouproject/cinemalist.php"];
    
    
    //[NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];  代表支持所有的接口类型
    //请求管理者 所有的网络请求,均有manager发起
    AFHTTPRequestOperationManager* manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain",@"text/json",@"application/json",@"text/javascript",@"text/html",nil];
    [manager GET:url_string parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSLog(@"=====%d", [NSThread isMainThread]);
        // 回到主线程
//        dispatch_async(dispatch_get_main_queue(), ^{
//            
//            
//            
//        });
        //        NSLog(@"使用AFN进行get请求 ===  %@",responseObject);
        self.allArray = [NSMutableArray array];
        
        NSMutableDictionary *dic = responseObject;
        NSDictionary *dic1 = [dic objectForKey:@"result"];
        NSMutableArray *array = [dic1 objectForKey:@"data"];
        
        for (NSDictionary *dic in array) {
            Cinema *cinema = [[Cinema alloc] init];
            [cinema setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:cinema];
            
//            NSLog(@"%ld", self.allArray.count);
            [cinema release];
            
        }
        
        
        [self.myTableView reloadData];
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"失败==== %@",error);
        
    }];
    
    
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
